from .gui.form.header import SimpleHeader
from .gui.form.button import BackButton
from .gui.app import GuiApp
from .gui.list import BaseListItem
from .gui import listscreen
from .gui.list.textitem import BasicTextItem
from episodes import CreateEpisodesMenu
from podcast import index

def CreatePodcastsMenu(app, current_song):
  # Create Podcasts screen
  header_cb = lambda screen: SimpleHeader(screen, "Podcasts",
      [BackButton(app, screen, 'Back', 'main menu')])
  screen, podcasts = listscreen.CreateListScreen(app, header_cb)
  def PlayPodcast(feed_id):
    def OnScreenChangeComplete():
      podcasts.ActionDone()
    def OnTap():
      app.UnlinkScreen("episodes")
      CreateEpisodesMenu(app, feed_id, current_song)
      app.SetCurrentScreen("episodes", GuiApp.ENTER_FROM_RIGHT,
          OnScreenChangeComplete)
      return BaseListItem.ON_TAP_UNHIGHLIGHT_LATER
    return OnTap

  items = []
  for feed_id in index.FeedIDs():
    feed = index.GetFeed(feed_id)
    if feed.HasDownloadedEpisodes():
      items.append(BasicTextItem(app, str(feed.title()),
        PlayPodcast(feed_id), is_submenu=True))
  items.sort()
  for item in items:
    podcasts.AddItem(item)

  app.AddScreen("podcasts", screen)

def ShowPodcasts(app, menu, current_song):
  app.UnlinkScreen("podcasts")
  CreatePodcastsMenu(app, current_song)
  def OnScreenChangeComplete():
    menu.ActionDone()
  app.SetCurrentScreen("podcasts", GuiApp.ENTER_FROM_RIGHT,
      OnScreenChangeComplete)
  return BaseListItem.ON_TAP_UNHIGHLIGHT_LATER


